package com.dc.service.impl;

import com.dc.base.em.ErrorMessageEnum;
import com.dc.base.vo.BaseModel;
import com.dc.base.vo.BusinessException;
import com.dc.dao.InventoryDao;
import com.dc.model.Inventory;
import com.dc.service.InventoryService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class InventoryServiceImpl implements InventoryService {
    @Autowired
    InventoryDao inventoryDao;
    public BaseModel selectPageInventory(Integer curr_page, Integer page_size, BaseModel baseModel,Integer inventory_id, String flower_name) {
        // 编写业务
        PageHelper.startPage(curr_page,page_size);
        // 开启分页以后需要马上接入查询语句，方便pageHelper实现分页
        List<Inventory> users=inventoryDao.selectPageInventory(inventory_id,flower_name);
        PageInfo pageInfo=new PageInfo(users,5);
        baseModel.setResultCode(0);
        baseModel.setMessage("分页查询租出成功！");
        baseModel.setData(pageInfo);
        return baseModel;
    }
    // 新增租出的接口
    public BaseModel addInventory(Inventory inventory, BaseModel baseModel) throws Exception {
        //调用新增租出的dao
        Integer result=inventoryDao.insertInventory(inventory);
        if(result<1){
            throw new BusinessException(ErrorMessageEnum.ADD_INVENTORY_ERROR);
        }
        baseModel.setResultCode(0);
        baseModel.setMessage("新增租出成功！");
        return baseModel;
    }

    // 修改租出的接口
    public BaseModel updateInventory(Inventory inventory, BaseModel baseModel) throws Exception {
        Integer result=inventoryDao.updateInventory(inventory);
        if(result<=0){
            throw  new BusinessException(1008,"修改租出失败");
        }
        baseModel.setResultCode(0);
        baseModel.setMessage("修改租出成功！");
        return baseModel;
    }

    // 删除租出的接口
//    public BaseModel deleteInventory(String ids, BaseModel baseModel) throws Exception {
//        String[] idArry=ids.split(",");
//        int result=inventoryDao.deleteInventory(idArry);
//        if(result<=0){
//            throw new BusinessException(1009,"删除"+result+"个租出失败。");
//        }
//        baseModel.setResultCode(0);
//        baseModel.setMessage("删除"+result+"个租出成功！");
//        return baseModel;
//    }

    // 根据id查询租出信息的接口
    public BaseModel selectInventoryById(Integer inventory_id,BaseModel baseModel) throws Exception{
        List<Inventory> inventoryList = inventoryDao.selectInventoryById(inventory_id);
        baseModel.setResultCode(0);
        baseModel.setMessage("查询单个租出成功");
        baseModel.setData(inventoryList);
        return baseModel;
    }
}
